

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * bu sinif tcp userinden belirtilen adrese belirtilen kapidan 
 * socket kullanarak erismeye calisir.
 * 
 * @author es
 *
 */
public class Server implements Runnable{
	private ServerSocket server ;
	public Server(){
		try {
			server = new ServerSocket(8900) ;
			new Thread(this).start() ;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * sunucuya erisilip erisilemedigini kontrol eden metoddur.
	 * @param IP : sunucunun IP adresi
	 * @param port : sunucunun dinledigi kapi numarasi
	 * @return sunucuya baglanilip baglanilmadigi bilgisini dondurur
	 * @throws HostNotFoundException 
	 */
	public void run(){		
		try {
			Socket s = server.accept() ;					
			InputStreamReader reader;
			PrintWriter writer ;
			try {
				reader = new InputStreamReader(s.getInputStream());
				writer= new PrintWriter(s.getOutputStream()) ;			
				BufferedReader br = new BufferedReader(reader) ;
				String line ;
				boolean ok = true ;
				while(ok){
					while((line = br.readLine()) != null){
						System.out.println(line);	
						if(line.split(",").length == 4){
							writer.println(line) ;
							writer.flush() ;
							System.out.println(line + "gonderildi");
						}						
					}				
				}
				writer.close() ;
				br.close() ;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		} catch (Exception e) {			
			e.printStackTrace() ;
		}		
	}
	
	public static void main(String[] args){
		Server conn = new Server() ;		
	}

}
